package com.vnp.vchat.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Handler;
import android.text.util.Linkify;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.telpoo.frame.delegate.Idelegate;
import com.vnp.vchat.R;
import com.vnp.vchat.UIControls.MyCircleImageView;
import com.vnp.vchat.UIControls.MyClickAbleImageView;
import com.vnp.vchat.bean.ObjMsgChat;
import com.vnp.vchat.util.PreferenceHelper;
import com.vnp.vchat.util.Utils;

public class ChatAdapter extends ArrayAdapter<ObjMsgChat> {

	public ChatAdapter(Context context, int resource, ArrayList<ObjMsgChat> objects) {
		super(context, resource, objects);
		this.context = context;
		preferenceHelper = new PreferenceHelper(context);
		this.objMsgChats = objects;
		this.resource = resource;
	}

	int resource;
	public static final int DELETATE_PLAY_AUDIO = 1001;
	public static final int DELEGATE_SHOW_IMAGE = 1002;
	Context context;
	PreferenceHelper preferenceHelper;
	ArrayList<ObjMsgChat> objMsgChats = new ArrayList<ObjMsgChat>();
	boolean isFirst = true;

	int positionPlay = -1;

	DisplayImageOptions optionsM = new DisplayImageOptions.Builder().cacheInMemory(false).cacheOnDisc(true).bitmapConfig(Config.RGB_565)
			.showImageForEmptyUri(R.drawable.defaul_avatar).build();

	DisplayImageOptions optionsF = new DisplayImageOptions.Builder().cacheInMemory(false).cacheOnDisc(true).bitmapConfig(Config.RGB_565)
			.showImageForEmptyUri(R.drawable.defaul_avatar).build();

	Hashtable<String, View> msgStatus = new Hashtable<String, View>();
	public SeekBar seekBarPlaying; // seekbar cuar item dang play nhac
	public int curPostionPlay;// luu vi tri dang play nhac
	private Idelegate idelegate;

	@Override
	public int getCount() {
		return objMsgChats.size();
	}

	@Override
	public ObjMsgChat getItem(int position) {
		return objMsgChats.get(position);
	}

	@Override
	public View getView(final int position, View v, ViewGroup parent) {

		final Holder holder;
		if (v == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			v = inflater.inflate(resource, parent, false);
			holder = new Holder();
			holder.mine = (ViewGroup) v.findViewById(R.id.mine);
			holder.friend = (ViewGroup) v.findViewById(R.id.friend);
			holder.imgAvataFriend = (MyCircleImageView) v.findViewById(R.id.avatar_khach_hang);
			holder.imgPhotoKH = (MyClickAbleImageView) v.findViewById(R.id.img_KH);
			holder.imgPhoto = (MyClickAbleImageView) v.findViewById(R.id.img_mine);
			holder.txtMsgKH = (TextView) v.findViewById(R.id.txt_msg_khach_hang);
			holder.txtMsg = (TextView) v.findViewById(R.id.txt_msg);
			holder.txtMyTime = (TextView) v.findViewById(R.id.txt_my_time);
			holder.txtTimeFriend = (TextView) v.findViewById(R.id.txt_friend_time);
			holder.icMyTime = (ImageView) v.findViewById(R.id.ic_my_time);
			holder.icFriendTime = (ImageView) v.findViewById(R.id.ic_friend_tiem);
			holder.txtStatus = (TextView) v.findViewById(R.id.status);
			holder.icError = (ImageView) v.findViewById(R.id.ic_error);

			holder.group_audio_kh = (ViewGroup) v.findViewById(R.id.group_audio_kh);
			holder.group_audio_me = (ViewGroup) v.findViewById(R.id.group_audio_me);
			holder.sbMe = (SeekBar) v.findViewById(R.id.sbMe);
			holder.tv_time_me = (TextView) v.findViewById(R.id.tv_time_me);
			holder.btnPlay = (ImageView) v.findViewById(R.id.btnPlay);
			holder.img_me = (MyCircleImageView) v.findViewById(R.id.avatar_me);
			holder.img_kh = (MyCircleImageView) v.findViewById(R.id.avatar_khach_hang);
			holder.nameKh = (TextView) v.findViewById(R.id.nameKh);
			// holder.sbKh = (SeekBar) v.findViewById(R.id.sbKh);
			// setfont
			holder.txtMsgKH.setTypeface(Utils.getTypefaceRegular(context));
			holder.txtMsg.setTypeface(Utils.getTypefaceRegular(context));
			holder.txtMyTime.setTypeface(Utils.getTypefaceLight(context));
			holder.txtTimeFriend.setTypeface(Utils.getTypefaceLight(context));
			holder.tv_time_me.setTypeface(Utils.getTypefaceLight(context));
			holder.nameKh.setTypeface(Utils.getTypefaceLight(context));
			v.setTag(holder);
		} else {
			holder = (Holder) v.getTag();
		}

		// holder.img_kh.setImageResource(R.drawable.defaul_avatar);
		// holder.img_me.setImageResource(R.drawable.defaul_avatar);
		final ObjMsgChat objMsgChat = objMsgChats.get(position);

		if (objMsgChat.getFromId().equals(preferenceHelper.getAdminId())) {
			// cua tao
			holder.img_me.setVisibility(View.GONE);
			holder.mine.setVisibility(View.VISIBLE);
			holder.friend.setVisibility(View.GONE);

			// check status
			switch (objMsgChat.getStatus()) {
			case 0:
				holder.icError.setVisibility(View.VISIBLE);
				holder.txtStatus.setVisibility(View.GONE);
				holder.icError.setImageResource(R.drawable.ic_danggui);
				break;
			case 1:
				holder.icError.setVisibility(View.GONE);
				holder.txtStatus.setVisibility(View.GONE);
				break;
			case 2:
				holder.icError.setVisibility(View.VISIBLE);
				holder.txtStatus.setVisibility(View.GONE);
				break;
			default:
				break;
			}

			if (position == 0) {
				holder.txtMsg.setBackgroundResource(R.drawable.blue_nhon);
				holder.img_me.setVisibility(View.VISIBLE);

			}
			holder.img_me.setVisibility(View.VISIBLE);
			ImageLoader.getInstance().displayImage(preferenceHelper.getMyAvatar(), holder.img_me, optionsM);

			if (position > 0) {
				if (objMsgChats.get(position - 1).getFromId().equals(objMsgChats.get(position).getFromId()))
					holder.img_me.setVisibility(View.GONE);
				if (objMsgChats.get(position - 1).getFromId().equals(preferenceHelper.getAdminId())) {
					holder.txtMsg.setBackgroundResource(R.drawable.bb_blue);
				} else {
					holder.txtMsg.setBackgroundResource(R.drawable.blue_nhon);
				}
			}
			if (objMsgChat.getType().equalsIgnoreCase("image")) {
				holder.imgPhoto.setVisibility(View.VISIBLE);
				holder.txtMsg.setVisibility(View.GONE);
				holder.imgPhoto.setPadding(15, 10, 10, 10);
				holder.img_me.setVisibility(View.VISIBLE);
				ImageLoader.getInstance().displayImage(objMsgChat.getUrlImageKH(), holder.imgPhoto, optionsM);
				holder.group_audio_me.setVisibility(View.GONE);
			}
			if (objMsgChat.getType().equalsIgnoreCase("text")) {
				holder.imgPhoto.setVisibility(View.GONE);
				holder.txtMsg.setVisibility(View.VISIBLE);
				holder.txtMsg.setText(objMsgChat.getContent());
				Linkify.addLinks(holder.txtMsg, Linkify.ALL);
				holder.txtMsg.setLinkTextColor(Color.WHITE);
				holder.group_audio_me.setVisibility(View.GONE);
			}
			if (objMsgChat.getType().equalsIgnoreCase("audio")) {
				holder.imgPhoto.setVisibility(View.GONE);
				holder.txtMsg.setVisibility(View.GONE);
				holder.group_audio_me.setVisibility(View.VISIBLE);

				// if(position== curPostionPlay){
				// this.seekBarPlaying=holder.sbMe;
				//
				// }

			}

		} else { // phan cua khach

			// set avatar
			holder.img_kh.setVisibility(View.VISIBLE);
			holder.nameKh.setVisibility(View.VISIBLE);
			ImageLoader.getInstance().displayImage(objMsgChat.getUrlAvatarKhachhang(), holder.img_kh, optionsM);

			if (position > 0) {
				if (objMsgChats.get(position - 1).getFromId().equals(objMsgChats.get(position).getFromId())) {
					holder.nameKh.setVisibility(View.GONE);
					holder.img_kh.setVisibility(View.GONE);
				}

			}

			holder.mine.setVisibility(View.GONE);
			holder.friend.setVisibility(View.VISIBLE);
			holder.nameKh.setText(objMsgChat.getTenKhachHang());
			if (position > 0) {

				if (objMsgChats.get(position - 1).getFromId().equals(preferenceHelper.getAdminId())) {
					holder.txtMsgKH.setBackgroundResource(R.drawable.bb_gray_nhon);
				} else {
					holder.txtMsgKH.setBackgroundResource(R.drawable.bb_gray);
				}

			} else {
				holder.txtMsgKH.setBackgroundResource(R.drawable.bb_gray_nhon);
			}

			// doi image

			if (objMsgChat.getType().equalsIgnoreCase(ObjMsgChat.TYPE_IMAGE)) {
				holder.txtMsgKH.setVisibility(View.GONE);
				holder.imgPhotoKH.setVisibility(View.VISIBLE);
				holder.imgPhotoKH.setPadding(10, 10, 15, 10);
				ImageLoader.getInstance().displayImage(objMsgChat.getUrlImageKH(), holder.imgPhotoKH, optionsF);
				holder.group_audio_kh.setVisibility(View.GONE);
			}
			if (objMsgChat.getType().equalsIgnoreCase(ObjMsgChat.TYPE_TEXT)) {
				holder.txtMsgKH.setVisibility(View.VISIBLE);
				holder.imgPhotoKH.setVisibility(View.GONE);
				holder.txtMsgKH.setText(objMsgChat.getContent());
				holder.group_audio_kh.setVisibility(View.GONE);
				Linkify.addLinks(holder.txtMsgKH, Linkify.ALL);
			}
			if (objMsgChat.getType().equalsIgnoreCase(ObjMsgChat.TYPE_AUDIO)) {
				holder.imgPhotoKH.setVisibility(View.GONE);
				holder.txtMsgKH.setVisibility(View.GONE);
				holder.group_audio_kh.setVisibility(View.VISIBLE);
			}
		}

		holder.imgPhoto.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				idelegate.callBack(objMsgChat.getUrlImageKH(), DELEGATE_SHOW_IMAGE);
				// showdialog(objMsgChat.getUrlImageKH());Toast.makeText(context,
				// "" + position, 0).show();

			}
		});

		holder.imgPhotoKH.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				idelegate.callBack(objMsgChat.getUrlImageKH(), DELEGATE_SHOW_IMAGE);
				// showdialog(objMsgChat.getUrlImageKH());Toast.makeText(context,
				// "" + position, 0).show();
			}
		});

		return v;
	}

	class Holder {
		ViewGroup friend, mine;
		MyCircleImageView imgAvataFriend, img_kh, img_me;
		TextView txtMsg, txtMsgKH, txtTimeFriend, txtMyTime, txtStatus;
		MyClickAbleImageView imgPhotoKH, imgPhoto;
		ImageView icMyTime, icFriendTime, icError, btnPlay;
		ViewGroup group_audio_kh, group_audio_me;
		SeekBar sbMe, sbKh;
		TextView tv_time_me, nameKh;
	}

	public void SetItems(ArrayList<ObjMsgChat> items) {
		objMsgChats.clear();
		objMsgChats.addAll(items);
		try {
			((Activity) context).runOnUiThread(new Runnable() {

				@Override
				public void run() {
					try {
						ChatAdapter.this.notifyDataSetChanged();
					} catch (Exception e) {
					}
				}
			});
		} catch (Exception e) {
		}
	}

	public void setDelegate(Idelegate idelegate) {
		this.idelegate = idelegate;

	}

}
